package com.heima.juc.n8;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author : leimingchao
 * @date Date : 2022年02月19日 16:45
 */
@Slf4j(topic = "c.TestThreadPoolExecutors")
public class TestThreadPoolExecutors {

    public static void main(String[] args) {

        //test1();
        test2();
    }

    private static void test2() {
        ExecutorService threadPool = Executors.newSingleThreadExecutor();


        threadPool.execute(() -> {
            log.debug("1");
            int i = 1 / 0;
        });

        threadPool.execute(() -> {
            log.debug("2");
        });

        threadPool.execute(() -> {
            log.debug("3");
        });
    }

    private static void test1() {
        ExecutorService threadPool = Executors.newFixedThreadPool(2, new ThreadFactory() {

            private AtomicInteger atomicInteger = new AtomicInteger(1);
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "mypool_t" + atomicInteger.getAndIncrement());
            }
        });


        threadPool.execute(() -> {
            log.debug("1");
        });

        threadPool.execute(() -> {
            log.debug("2");
        });

        threadPool.execute(() -> {
            log.debug("3");
        });
    }
}
